package com.company.bookstore.controller.dataExport_module;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.bookstore.common.formatUtils.FormatUtils;
import com.company.bookstore.common.result.MvcResult;
import com.company.bookstore.entities.book_module.Book;
import com.company.bookstore.entities.book_module.BookImg;
import com.company.bookstore.entities.cart_module.Cart;
import com.company.bookstore.entities.order_module.Order;
import com.company.bookstore.service.book_module.BookService;
import com.company.bookstore.service.dataExport_module.DataExportService;
import com.company.bookstore.service.order_module.OrderService;
import com.company.bookstore.service.user_admin_module.AddressService;
import com.company.bookstore.service.user_admin_module.UserService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Api(tags = "系统：数据导出")
@Slf4j
@RestController
@RequestMapping("/api/export")
public class DataExportController {

    @Resource
    BookService bookService;

    @Resource
    OrderService orderService;

    @Resource
    UserService userService;

    @Resource
    AddressService adressService;

    @Resource
    DataExportService dataExportService;

    @PostMapping("/downloadOrder")
    public void downloadAllOrder(HttpServletResponse response) throws IOException {
        List<Order> orderList = orderService.list();
        if(!orderList.isEmpty()){
            for(Order order:orderList){
                order.setBook(bookService.getById(order.getFkBookId()));
                order.setUser(userService.getById(order.getFkUserId()));
                order.setAddress(adressService.getById(order.getOrderAdd()));
            }
    }
        dataExportService.downloadOrder(orderList,response);
    }

    @PostMapping("/downloadBook")
    public void downloadAllBook(HttpServletResponse response) throws IOException {
        dataExportService.downloadBook(bookService.list(),response);
    }

    @PostMapping("/downloadUser")
    public void downloadAllUser(HttpServletResponse response) throws IOException {
        dataExportService.downloadUser(userService.list(),response);
    }
}
